Reportgroupssummaryhandler Usage
| Document Version | v.2 |
|---|---|
| Document Last Updated | 2/3/2020 |
| Software Version Documented | v9.5.83 |
How/Why to use the ReportGroupsSummaryHandler to summarize totals
XtraReports does not support doing summary operations on group totals, for example if you have an invoice list with Grand Total field for each invoice, and as a child group we have all payments done for this invoice, now for the total of the report if you just did SUM(GrandTotal) it will cause the sum function to duplicate the GrandTotal of the invoice for all of its payments making the total off. They support custom events for this but it is very tedious and error prone since for each field you need to handle 3 events per group and make sure things are ordered correctly so you can reset the group total correctly, also that applies for adding custom summary functions like GM% or similar things.
Reasons to use ReportGroupsSummaryHandler:
-
Don’t have to duplicate code.
-
Less error prone
-
Less code
Usage:
- To access the area to enter the script go to Report Designer -> Scripts
-
The class: ReportGroupsSummaryHandler
-
It exposes AddSummaryField method: This method is used to register columns and their summary fields.
-
It takes 2 parameters:
-
ValueProvider: Which is a function that returns the value you want to do the summary for.
-
AccFactory: Which is a function that works as a factory method for the Accumulator.
-
-
The function returns SummaryField which can be used to add grouping levels to it.
-
This class supports 2 modes:
-
First mode which just works as an accumulator without the need to provide the events XtraReports used to handle custom summary functions
-
Second mode which is used to avoid duplicates by passing a function to the constructor that returns a value (or a tuple of values, or whatever class that supports equality), the return of the function represents the key that is used to detect duplicate entries (e.g. in the example mentioned above it will be OrderShipperHeaderSys).
-
-
-
The class: SummaryField
-
It exposes AddFieldLevels method: It is used to register field level summary controls, these are the labels that are used to display summary values. You can register as many levels as you want. A level is basically a group that contains this field.
-
It takes 1 parameter:
- ctrls: Enumerable of XRLabel.
-
Example:
-
Create a new object of column type with no filter.
- var summaryHandler = new Reporting.ReportGroupsSummaryHandler();
-
Create a new object of column type and use column to filter out duplicates.
- var summaryHandler = new Reporting.ReportGroupsSummaryHandler<int>(baseReport1, () => (int)GetCUrrentCOlumnValue(“OrderSys”));
-
Then add the column you want the summary from.
- ()=> GetCurrentColumnValue("OrderTotal"),
-
Then add the field you would like to display the summary.
- .AddFieldLevels(new []{tableCell18});
-
Full Code:
- private void baseReport1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
var summaryHandler = new Reporting.ReportGroupsSummaryHandler<int>(baseReport1, () => (int)GetCurrentColumnValue("OrderSys"));
summaryHandler .AddSummaryField(
()=> GetCurrentColumnValue("OrderTotal"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{tableCell18});
}
-
Another example taken from Invoicing and Crediting Summary by Account Rep system report.
-
private void baseReport1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
var summaryHandler = new Reporting.ReportGroupsSummaryHandler();
var noDupsSummaryHandler = new Reporting.ReportGroupsSummaryHandler<Tuple<int,int>>(baseReport1, () => Tuple.Create((int)GetCurrentColumnValue("SubOrderSys"), (int)GetCurrentColumnValue("IsCredit")));noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("Cost"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumCost3,SumCost4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("SubTotal"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumSubTotal3,SumSubTotal4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("TaxTotal"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumTaxTotal3,SumTaxTotal4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("FreightCharge"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumFreight3,SumFreight4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("GrandTotal"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumGrandTotal3,SumGrandTotal4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("MiscCharge"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumMiscCharge3,SumMiscCharge4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("TermsDiscount"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumTermsDiscount3,SumTermsDiscount4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("Discount"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumDiscounts3,SumDiscounts4});
noDupsSummaryHandler.AddSummaryField(
()=> GetCurrentColumnValue("ServiceCharge"),
() => new Reporting.SumAccumilator<decimal>()
).AddFieldLevels(new []{SumService3,SumService4});summaryHandler.AddSummaryField<decimal,Tuple<decimal,decimal>>(
()=> Tuple.Create((decimal)(GetCurrentColumnValue("ProfitSalesTotal")??0.0M), (decimal)(GetCurrentColumnValue("Cost")??0.0M)),
() => new Reporting.GrandMarginAccumilator<decimal>()
).AddFieldLevels(new []{GMTotal2});noDupsSummaryHandler.AddSummaryField<decimal,Tuple<decimal,decimal>>(
()=> Tuple.Create((decimal)(GetCurrentColumnValue("ProfitSalesTotal")??0.0M), (decimal)(GetCurrentColumnValue("Cost")??0.0M)),
() => new Reporting.GrandMarginAccumilator<decimal>()
).AddFieldLevels(new []{GMTotal3,GMTotal4});
}